﻿using DataCenter.Models;
using DataCenter_DataBase;
using KeaoBase;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace DataCenter.Controllers
{
    public class QRController : Controller
    {
        // GET: QR
        public string Index()
        {
            string b = KeaoHttpUtils.HttpGetStringParam(System.Web.HttpContext.Current, "b");
            string u = KeaoHttpUtils.HttpGetStringParam(System.Web.HttpContext.Current, "u");
            string l = KeaoHttpUtils.HttpGetStringParam(System.Web.HttpContext.Current, "l");
            string f = KeaoHttpUtils.HttpGetStringParam(System.Web.HttpContext.Current, "f");
            string i = KeaoHttpUtils.HttpGetStringParam(System.Web.HttpContext.Current, "i");

            if (string.IsNullOrEmpty(u))
            {
                return "参数错误";
            }

            ApiDataBase m_Api = new ApiDataBase();
            tbl_client clientItem = m_Api.GetClientByCode(u);
            if (clientItem == null)
            {
                return "参数错误";
            }

            BaseDataBase m_DB = System.Reflection.Assembly.Load("KeaoDataBase").CreateInstance(string.Format("KeaoDataBase.{0}", clientItem.datasource_type)) as BaseDataBase;
            if (m_DB.InitDataBase(clientItem.datasource_string))
            {
                ///
                /// 验证该二维码是否已经验证过
                ///
                string createCondition = string.Empty;

                if (!string.IsNullOrEmpty(i))
                {
                    createCondition += m_DB.CreateAndCondition("id", DataValueCommonType.字符串, i, "", SearchConditionType.等于);
                }
                else if (!string.IsNullOrEmpty(l) && !string.IsNullOrEmpty(b))
                {
                    createCondition += m_DB.CreateAndCondition("kehubianhao", DataValueCommonType.字符串, u, "", SearchConditionType.等于);
                    createCondition += m_DB.CreateAndCondition("zhandian", DataValueCommonType.字符串, l, "", SearchConditionType.等于);
                    createCondition += m_DB.CreateAndCondition("bangfang", DataValueCommonType.字符串, f, "", SearchConditionType.等于);
                    createCondition += m_DB.CreateAndCondition("chengzhongbianhao", DataValueCommonType.字符串, b, "", SearchConditionType.等于);
                }
                else
                {
                    return "参数错误";
                }

                DataTable yanZhengDt = m_DB.GetDataTableByCondition("tbl_yanzheng", createCondition.Replace("id","chengzhongid"));
                if (yanZhengDt != null && yanZhengDt.Rows.Count > 0)
                {
                    var result = new { msg = "此单据已验证过！",
                        data = yanZhengDt.AsEnumerable().Select(item => new
                        {
                            danhao = item["chengzhongbianhao"],
                            maozhongshijian = item["maozhongshijian"],
                            pizhongshijian = item["pizhongshijian"],
                            danweimingcheng = item["danweimingcheng"],
                            wuliaomingcheng = item["wuliaomingcheng"],
                            xinghao = item["xinghaomingcheng"],
                            maozhong = item["maozhong"],
                            pizhong = item["pizhong"],
                            kouzhong = item["kouzhong"],
                            jingzhong = item["jingzhong"],
                        }).ToList()
                    };
                    return KeaoUtils.Obj2Json(result);
                }
                DataTable Dt = m_DB.GetDataTableByCondition("tbl_chengzhong", createCondition);
                if (Dt != null && Dt.Rows.Count > 0)
                {
                    TableRow newRow = new TableRow("tbl_yanzheng", Guid.NewGuid().ToString(), DBCtl.插入);
                    newRow.DataCols = new List<TableCol>() {
                        new TableCol() { ColName = "kehubianhao", ValueMember=Dt.Rows[0]["kehubianhao"], ValueType = DataValueCommonType.字符串 },
                        new TableCol() { ColName = "bangfang", ValueMember=Dt.Rows[0]["bangfang"], ValueType = DataValueCommonType.字符串 },
                        new TableCol() { ColName = "zhandian", ValueMember=Dt.Rows[0]["zhandian"], ValueType = DataValueCommonType.字符串 },
                        new TableCol() { ColName = "chengzhongbianhao", ValueMember=Dt.Rows[0]["chengzhongbianhao"], ValueType = DataValueCommonType.字符串 },
                        new TableCol() { ColName = "danweimingcheng", ValueMember=Dt.Rows[0]["danweimingcheng"], ValueType = DataValueCommonType.字符串 },
                        new TableCol() { ColName = "wuliaomingcheng", ValueMember=Dt.Rows[0]["wuliaomingcheng"], ValueType = DataValueCommonType.字符串 },
                        new TableCol() { ColName = "xinghaomingcheng", ValueMember=Dt.Rows[0]["xinghao"], ValueType = DataValueCommonType.字符串 },
                        new TableCol() { ColName = "chepaihao", ValueMember=Dt.Rows[0]["chepaihao"], ValueType = DataValueCommonType.字符串 },
                        new TableCol() { ColName = "maozhong", ValueMember=Dt.Rows[0]["maozhong"], ValueType = DataValueCommonType.数字 },
                        new TableCol() { ColName = "pizhong", ValueMember=Dt.Rows[0]["pizhong"], ValueType = DataValueCommonType.数字 },
                        new TableCol() { ColName = "kouzhong", ValueMember=Dt.Rows[0]["kouzhong"], ValueType = DataValueCommonType.数字 },
                        new TableCol() { ColName = "jingzhong", ValueMember=Dt.Rows[0]["jingzhong"], ValueType = DataValueCommonType.数字 },
                        new TableCol() { ColName="chengzhongleixing", ValueMember=Dt.Rows[0]["chengzhongleixing"], ValueType= DataValueCommonType.字符串},
                        new TableCol() { ColName="maozhongshijian", ValueMember=Dt.Rows[0]["maozhongshijian"], ValueType= DataValueCommonType.字符串},
                        new TableCol() { ColName="pizhongshijian", ValueMember=Dt.Rows[0]["pizhongshijian"], ValueType= DataValueCommonType.字符串},
                        new TableCol() { ColName="chengzhongid", ValueMember=Dt.Rows[0]["id"], ValueType= DataValueCommonType.字符串}

                    };
                    m_DB.SaveTableRows(newRow);
                    Dictionary<string, string> dic = new Dictionary<string, string>();

                    var result = new
                    {
                        msg = "验证通过",
                        data = Dt.AsEnumerable().Select(item => new
                        {
                            danhao = item["chengzhongbianhao"],
                            maozhongshijian = item["maozhongshijian"],
                            pizhongshijian = item["pizhongshijian"],
                            danweimingcheng = item["danweimingcheng"],
                            wuliaomingcheng = item["wuliaomingcheng"],
                            xinghao = item["xinghao"],
                            maozhong = item["maozhong"],
                            pizhong = item["pizhong"],
                            kouzhong = item["kouzhong"],
                            jingzhong = item["jingzhong"],
                        }).ToList()
                    };
                    return KeaoUtils.Obj2Json(result);
                }
                else
                {
                    return KeaoUtils.Obj2Json(new { msg = "没有找到数据" });
                }
            }
            else
            {
                return KeaoUtils.Obj2Json(new { msg = "查询失败" });
            }
        }
    }
}